
<h1><span class="yiyi-st" id="yiyi-12">numpy.result_type</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.result_type.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.result_type.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.result_type"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.</code><code class="descname">result_type</code><span class="sig-paren">(</span><em>*arrays_and_dtypes</em><span class="sig-paren">)</span></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">返回将NumPy类型升级规则应用于参数所产生的类型。</span></p>
<p><span class="yiyi-st" id="yiyi-15">NumPy中的类型升级与C ++等语言中的规则的工作方式类似，但略有不同。</span><span class="yiyi-st" id="yiyi-16">当使用标量和数组时，数组的类型优先，并且考虑标量的实际值。</span></p>
<p><span class="yiyi-st" id="yiyi-17">例如，计算3 * a，其中a是32位浮点的数组，直观地应导致32位浮点数输出。</span><span class="yiyi-st" id="yiyi-18">如果3是32位整数，则NumPy规则指示它不能无损地转换为32位浮点，因此64位浮点应该是结果类型。</span><span class="yiyi-st" id="yiyi-19">通过检查常量&apos;3&apos;的值，我们可以看到它适合一个8位整数，它可以无损地转换为32位浮点数。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-20">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-21"><strong>arrays_and_dtypes</strong>：数组和dtypes的列表</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-22">需要结果类型的某些操作的操作数。</span></p>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-23">返回：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-24"><strong>out</strong>：dtype</span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-25">结果类型。</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-26">也可以看看</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-27"><a class="reference internal" href="numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-obj docutils literal"><span class="pre">dtype</span></code></a>，<a class="reference internal" href="numpy.promote_types.html#numpy.promote_types" title="numpy.promote_types"><code class="xref py py-obj docutils literal"><span class="pre">promote_types</span></code></a>，<a class="reference internal" href="numpy.min_scalar_type.html#numpy.min_scalar_type" title="numpy.min_scalar_type"><code class="xref py py-obj docutils literal"><span class="pre">min_scalar_type</span></code></a>，<a class="reference internal" href="numpy.can_cast.html#numpy.can_cast" title="numpy.can_cast"><code class="xref py py-obj docutils literal"><span class="pre">can_cast</span></code></a></span></p>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-28">笔记</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-29"><span class="versionmodified">版本1.6.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-30">使用的具体算法如下。</span></p>
<p><span class="yiyi-st" id="yiyi-31">通过首先检查布尔值，整数（int / uint）或浮点（float / complex）中所有数组和标量的最大种类来确定类别。</span></p>
<p><span class="yiyi-st" id="yiyi-32">如果只有标量或标量的最大类别高于数组的最大类别，则数据类型与<a class="reference internal" href="numpy.promote_types.html#numpy.promote_types" title="numpy.promote_types"><code class="xref py py-func docutils literal"><span class="pre">promote_types</span></code></a>组合以生成返回值。</span></p>
<p><span class="yiyi-st" id="yiyi-33">否则，对每个数组调用<a class="reference internal" href="numpy.min_scalar_type.html#numpy.min_scalar_type" title="numpy.min_scalar_type"><code class="xref py py-obj docutils literal"><span class="pre">min_scalar_type</span></code></a>，并将生成的数据类型与<a class="reference internal" href="numpy.promote_types.html#numpy.promote_types" title="numpy.promote_types"><code class="xref py py-func docutils literal"><span class="pre">promote_types</span></code></a>组合以生成返回值。</span></p>
<p><span class="yiyi-st" id="yiyi-34">int值集合不是具有相同位数的类型的uint值的子集，某些不在<a class="reference internal" href="numpy.min_scalar_type.html#numpy.min_scalar_type" title="numpy.min_scalar_type"><code class="xref py py-func docutils literal"><span class="pre">min_scalar_type</span></code></a>中反映，但在<a class="reference internal" href="#numpy.result_type" title="numpy.result_type"><code class="xref py py-obj docutils literal"><span class="pre">result_type</span></code></a>中作为特殊情况处理。</span></p>
<p class="rubric"><span class="yiyi-st" id="yiyi-35">例子</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">result_type</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;i1&apos;</span><span class="p">))</span>
<span class="go">dtype(&apos;int8&apos;)</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">result_type</span><span class="p">(</span><span class="s1">&apos;i4&apos;</span><span class="p">,</span> <span class="s1">&apos;c8&apos;</span><span class="p">)</span>
<span class="go">dtype(&apos;complex128&apos;)</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">result_type</span><span class="p">(</span><span class="mf">3.0</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">)</span>
<span class="go">dtype(&apos;float64&apos;)</span>
</pre></div>
</div>
</dd></dl>
